<?php

namespace IDock\Insurance\TAIC\Response;

use IDock\Http\Response;

class AllTAICResponse extends Response
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return  $this;
        }
        $data = json_decode($body,true);

        if ($this->printer and  is_string($this->printer)) {
            $this->printer = new $this->printer;
        }

        $resultCode = $data['resultDTO']['resultCode'] ?? '';
        $resultMess = $data['resultDTO']['resultMess'] ?? '';

        if($resultCode != 'SUCCESS') {
            $this->setError($resultMess);
            return $this;
        }

        $dealFlag = $data['dealFlag'] ?? '';
        $dealMess = $data['dealMessage'] ?? '';
        $dealMass = $data['dealMassage'] ?? '';

        if ($dealFlag != '1') {
            $this->setError($dealMess.$dealMass);
            return $this;
        }

        $this->setBody($data);

        return $this;
    }
}
